﻿<!-- 正文开始 -->
<style >
     .layui-table-cell{
         height: auto!important;
         white-space: normal;
     }
</style>

<!-- 正文开始 -->
<div class="layui-fluid">
	<div class="layui-card">
	    <div class="layui-card-body" >
	    	<div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" id="btnAddVersion">
                            <i class="layui-icon">&#xe654;</i>添加
                        </button>
                    </div>
                </div>
            </div>
	        <table class="layui-table" id="tableversion" lay-filter="tableversion"></table>
	    </div>
	</div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBarversion">
	<a style="padding-top: 2px;height: 25px;margin-bottom: 2px;" class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
	<a style="padding-top: 2px;height: 25px;margin-bottom: 2px;" class="layui-btn layui-btn-normal layui-btn-xs" lay-event="files">附件</a>
    <a style="padding-top: 2px;height: 25px;margin-bottom: 2px;" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="modelVersionScript">
    <form id="modelVersionForm" lay-filter="modelVersionForm" class="layui-form model-form">
		<input name="id" type="hidden"/>
		<div class="layui-form-item">
            <label class="layui-form-label">版本号</label>
            <div class="layui-input-block">
                <input name="number" placeholder="请输入版本号" type="number" class="layui-input" min="0" max="1000" step='0.01'
                       lay-verType="tips" lay-verify="required|number" required/>
            </div>
        </div>
	   <div class="layui-form-item">
		    <label class="layui-form-label">状态</label>
		    <div class="layui-input-block">
		      <select name="status" lay-filter="status">
		        <option value="0">未发布</option>
		        <option value="1">已发布</option>
		        <option value="2">已过时</option>
		      </select>
		    </div>
	    </div>	
        <div class="layui-form-item">
            <label class="layui-form-label">说明</label>
            <div class="layui-input-block">
                <textarea name="remark" placeholder="请输入内容" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button type="button" class="layui-btn" lay-filter="modelSubmitVersion" lay-submit>保存</button>
        </div>
    </form>
</script>


<!-- 表单弹窗 -->
<script type="text/html" id="modelVersionFilesTableScript">
	<div class="layui-card">
	    <div class="layui-card-body" >
	    	<div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" id="uploadSoftFilesBtn">
                            <i class="layui-icon">&#xe654;</i>上传
                        </button>
                    </div>
                </div>
            </div>
	        <table class="layui-table" id="softFilesVersionTable" lay-filter="softFilesVersionTable"></table>
	    </div>
	</div>
</script>


<script type="text/html" id="tableFilesBarScript">
    <a style="padding-top: 2px;height: 25px;margin-bottom: 2px;" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- js部分 -->
<script>
layui.use(['layer', 'form', 'table', 'util', 'admin','upload'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var config = layui.config;
        var upload = layui.upload;
        
        var insTb = table.render({
            elem: '#tableversion',
            url: '#(ctx)/biz/version/listData',
            page: true,
            cols: [[
                {type: 'numbers'},
                {field: 'number', sort: false, title: '版本号'},
                {field: 'status', sort: false, title: '状态',templet: function (d) {
                	if(d.status=='0'){
                		return '待发布';
                	}
                	else if(d.status=='1'){
                		return '已发布';
                	}
                	else{
                		return '已过时';
                	}
				}},
                {field: 'remark', sort: false, title: '说明'},
                {field: 'createTime', sort: false, title: '创建时间'},
                {align: 'center', toolbar: '#tableBarversion',width:160, title: '操作'}
            ]]
        });
         // 添加
        $('#btnAddVersion').click(function () {
        	showEditModel();
        });

        // 工具条点击事件
        table.on('tool(tableversion)', function (obj) {	
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'del') { // 删除
                doDel(obj);
            }
            else if(layEvent === 'edit'){
            	showEditModel(data);
            }
            else if(layEvent === 'files'){
            	showFilesModel(data);
            }
            
        });
        
        function showEditModel(mRole) {
            admin.open({
                type: 1,
                title: (mRole ? '编辑' : '新增'),
                content: $('#modelVersionScript').html(),
                offset: 't'	,
                success: function (layero, dIndex) {
                    var url = 'biz/version/save';
                    form.val('modelVersionForm', mRole); 
                    form.on('submit(modelSubmitVersion)', function (data) {
                        layer.load(2);
                        var formData={}; 
                        for(var key in data.field){
                        	formData['version.'+key]=data.field[key];
                        }
                        admin.req(url, formData, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.close(dIndex);
                                layer.msg(res.message, {icon: 1});
                                insTb.reload({}, 'data');
                            } else {
                                layer.msg(res.message, {icon: 2});
                            }
                        }, 'get');
                        return false;
                    });
                }
            });
        }
        
        
        // 显示编辑弹窗
/*         function showEditModel() {
            admin.open({
                type: 1,
                title: '添加版本',
                content: $('#modelVersion').html(),
                area:'400px',
                success: function (layero, dIndex) {
                	form.render();
                 	upload.render({
			      		elem: '#file-id',
			      		url: '#(ctx)/biz/version/upload',
			      		accept: 'file',
			      		auto: false,
			      		bindAction: '#modelSubmitVersion',
			      		before:function (obj){
			      			this.data = form.val('modelVersionForm');
			      		},
			      		done: function(res, index, upload) {
			      			layer.closeAll('loading'); //关闭loading
			      			if (200 == res.code) {
			      			layer.close(dIndex);
			      				layer.msg(res.message, {
			      					picture: 1
			      				});
			      				insTb.reload();
			      			} else {
			      				layer.msg(res.message, {
			      					picture: 2
			      				});
			      			}
			      		},
			      		error: function(index, upload) {
			      			layer.closeAll('loading');
			      			layer.msg("上传失败！", {
			      				picture: 2
			      			});
			      		}
			      	}); 
                }
            });
        } */
       	
        // 删除
        function doDel(obj) {
            layer.confirm('确定要删除吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('biz/version/delete', {
                    id: obj.data.id
                }, function (res) {
                    layer.closeAll('loading');
                    if (200==res.code) {
                        layer.msg(res.message, {picture: 1});
                        insTb.reload();
                    } else {
                        layer.msg(res.message, {picture: 2});
                    }
                }, 'get');
            });
        }
        
        function showFilesModel(data){
            admin.open({
                type: 1,
                title: '升级清单',
                content: $('#modelVersionFilesTableScript').html(),
                offset: 't'	,
                area: ['800px', '600px'],
                success: function (layero, dIndex) {
                    
                 	upload.render({
			      		elem: '#uploadSoftFilesBtn',
			      		url: '#(ctx)/biz/version/upload?versionId='+data.id,
			      		accept: 'file',
			      		auto: true,
			      		bindAction: '#modelSubmitVersion',
			      		before:function (obj){
			      			this.data = form.val('modelVersionForm');
			      		},
			      		done: function(res, index, upload) {
			      			layer.closeAll('loading'); //关闭loading
			      			if (200 == res.code) {
			      				layer.msg(res.message, {
			      					picture: 1
			      				});
			      				insFilesTb.reload();
			      			} else {
			      				layer.msg(res.message, {
			      					picture: 2
			      				});
			      			}
			      		},
			      		error: function(index, upload) {
			      			layer.closeAll('loading');
			      			layer.msg("上传失败！", {
			      				picture: 2
			      			});
			      		}
			      	});
                 	
                    var insFilesTb = table.render({
                        elem: '#softFilesVersionTable',
                        url: '#(ctx)/biz/version/listDataForFiles?versionId='+data.id,
                        page: true,
                        height:'450px',
                        cols: [[
                            {type: 'numbers'},
                            {field: 'fileName', sort: false, title: '文件名称'},
                            {field: 'format', sort: false, title: '文件后缀'},
                            {field: 'targetPath', sort: false, title: '安装路径',edit: 'text'},
                            {align: 'center', toolbar: '#tableFilesBarScript',width:80, title: '操作'}
                        ]]
                    });
                    
                  //监听单元格编辑
                   table.on('edit(softFilesVersionTable)', function(obj){
                      var value = obj.value //得到修改后的值
                      ,data = obj.data //得到所在行所有键值
                      ,field = obj.field; //得到字段
                      layer.load(2);
                      var formData={}; 
                      formData['versionFiles.id']=data.id;
                      formData['versionFiles.targetPath']=value;
                      admin.req("biz/version/saveFiles", formData, function (res) {
                          layer.closeAll('loading');
                          if (res.code == 200) {
                              layer.msg(res.message, {icon: 1});
                          } else {
                              layer.msg(res.message, {icon: 2});
                              insFilesTb.reload();
                          }
                      }, 'get');
                      
                    });
                    
                    table.on('tool(softFilesVersionTable)', function (obj) {	
                        var data = obj.data;
                        var layEvent = obj.event;
                        if (layEvent === 'del') { // 删除
                        	deleteFiles(obj);
                        }
                        else if(layEvent === 'edit'){
                        	showEditModel(data);
                        }
                        else if(layEvent === 'files'){
                        	showFilesModel(data);
                        }
                    });
                    
                    function deleteFiles(obj) {
                        layer.confirm('确定要删除吗？', {
                            skin: 'layui-layer-admin',
                            shade: .1
                        }, function (i) {
                            layer.close(i);
                            layer.load(2);
                            admin.req('biz/version/deleteFiles', {
                                id: obj.data.id
                            }, function (res) {
                                layer.closeAll('loading');
                                if (200==res.code) {
                                    layer.msg(res.message, {picture: 1});
                                    insFilesTb.reload();
                                } else {
                                    layer.msg(res.message, {picture: 2});
                                }
                            }, 'get');
                        });
                    }
                }
            });
        }
        
        
        
    });
</script>